use vertica_rs::{Client, ConnectionConfig};

// Note: These tests require a running Vertica instance
// They should be run with: cargo test --test integration_test -- --ignored
use vertica_rs::Value;
#[tokio::test]
#[ignore = "Requires running Vertica instance"]
async fn test_basic_connection() {
    let config = ConnectionConfig::from_url("vertica://dbadmin:password@localhost:5433/docker").unwrap();
    let client = Client::new(config).await.unwrap();
    
    client.query("select 1").await.unwrap();
}

#[tokio::test]
#[ignore = "Requires running Vertica instance"]
async fn test_simple_query() {
    let config = ConnectionConfig::from_url("vertica://dbadmin:password@localhost:5433/docker").unwrap();
    let client = Client::new(config).await.unwrap();
    
    let result = client.query("SELECT 1 as test_value").await.unwrap();
    assert_eq!(result.len(), 1);
    
    let row = result.rows().first().unwrap();
    let value: Value = row.get(0).unwrap().clone();
    assert_eq!(value, Value::Int(1));
}

#[tokio::test]
#[ignore = "Requires running Vertica instance"]
async fn test_query_with_params() {
    let config = ConnectionConfig::from_url("vertica://dbadmin:password@localhost:5433/docker").unwrap();
    let client = Client::new(config).await.unwrap();
    
    let result = client.execute_with_params(
        "SELECT 42 as param_value", 
        &[Value::Int(42)]
    ).await.unwrap();
    
    assert_eq!(result, 1);
}

#[tokio::test]
#[ignore = "Requires running Vertica instance"]
async fn test_transaction() {
    let config = ConnectionConfig::from_url("vertica://dbadmin:password@localhost:5433/docker").unwrap();
    let client = Client::new(config).await.unwrap();
    
    // Create test table
    client.execute_with_params("CREATE LOCAL TEMP TABLE test_tx (id INTEGER, name VARCHAR(50))",&vec![]).await.unwrap();
    
    // Test transaction
    let mut tx = client.transaction().await.unwrap();
    
    tx.execute(
        "INSERT INTO test_tx VALUES (1,'Test User')" ,&vec![]
    ).await.unwrap();
    
    // Verify data exists in transaction
    let result = tx.query("SELECT * FROM test_tx").await.unwrap();
    assert_eq!(result.len(), 1);
    
    tx.commit().await.unwrap();
    
    // Verify data persists after commit
    let result = client.query("SELECT * FROM test_tx").await.unwrap();
    assert_eq!(result.len(), 1);
}

#[tokio::test]
#[ignore = "Requires running Vertica instance"]
async fn test_connection_pool() {
    let config = ConnectionConfig::from_url("vertica://dbadmin:password@localhost:5433/docker").unwrap();
    let client = Client::new(config).await.unwrap();
    
    // Test multiple queries
    for i in 0..5 {
        let result = client.execute_with_params(
            "SELECT ? as num", 
            &[Value::Int(i)]
        ).await.unwrap();
        
        assert_eq!(result, i as u64);
    }
}

#[tokio::test]
#[ignore = "Requires running Vertica instance"]
async fn test_data_types() {
    let config = ConnectionConfig::from_url("vertica://dbadmin:password@localhost:5433/docker").unwrap();
    let client = Client::new(config).await.unwrap();
    
    let result = client.query("SELECT 
        42 as int_val,
        3.14 as float_val,
        'hello' as string_val,
        true as bool_val,
        NULL as null_val
    ").await.unwrap();
    
    assert_eq!(result.len(), 1);
    let row = result.rows().first().unwrap();
    
    let int_val: Value = row.get(0).unwrap().clone();
    assert_eq!(int_val, Value::Int(42));
    
    let float_val: Value = row.get(1).unwrap().clone();
    assert!(matches!(float_val, Value::Float(f) if (f - 3.14).abs() < 0.001));
    
    let string_val: Value = row.get(2).unwrap().clone();
    assert_eq!(string_val, Value::String("hello".to_string()));
    
    let bool_val: Value = row.get(3).unwrap().clone();
    assert_eq!(bool_val, Value::Boolean(false));
    
    let null_val: Value = row.get(4).unwrap().clone();
    assert_eq!(null_val, Value::Null);
}